package com.indigon.parkStory.view.objects
{
	import com.indigon.parkStory.model.objects.FrogModel;
	import com.indigon.parkStory.model.objects.LillyModel;
	
	import flash.display.BlendMode;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;

	public class FrogView extends ObjectView
	{
		private static const DROWN_ALPHA_REDUCTION : Number = 0.03;
		private static const DROWN_SCALE_REDUCTION : Number = 0.005;
		private static const FADER_SCALE_CHANGE    : Number = 0.01;	
		
		private static const JUMP_LABEL : String = "jump";
		private static const IDLE_LABEL : String = "idle";
		
		private static const JUMP_SCALE : Number = 0.25;
		public function FrogView(frog:FrogModel)
		{
			super(new GFX_Frog(), frog);
		}
		
		public override function set(container:DisplayObjectContainer, x:Number, y:Number, rotation:Number=0, layer:uint=0):void
		{
			super.set(container, x, y, Math.random() * 360, container.numChildren);
		}
		
		public function jump():void
		{
			var frog  : FrogModel = FrogModel(_object);
			graphic.scaleX = graphic.scaleY = 1;
			graphic.gotoAndStop(JUMP_LABEL);
			graphic.rotation = (frog.jumpAng * 180 / Math.PI) + 90;
		}
		
		public function land():void
		{
			graphic.gotoAndStop(IDLE_LABEL);
		}

		
		public override function update():void
		{
			var frog  : FrogModel = FrogModel(_object);
			
			if (frog.isDead && (graphic.alpha > 0))
			{
				graphic.alpha  -= DROWN_ALPHA_REDUCTION;
				graphic.scaleX -=  DROWN_SCALE_REDUCTION;
				graphic.scaleY -=  DROWN_SCALE_REDUCTION;
			}
			else
			{
				var lilly : LillyModel = frog.lilly;
				
				if (lilly != null)
				{
					graphic.rotation += lilly.angVel;
					if (lilly.data.isFading)
					{
						graphic.scaleX -= (FADER_SCALE_CHANGE * (1 - lilly.fadeValue));
						graphic.scaleY -= (FADER_SCALE_CHANGE * (1 - lilly.fadeValue));		
					}
				}
				else
				{
					graphic.scaleX = graphic.scaleY = 1 + jumpEase(frog.jumpArrival);
				}
			}
			
			place(frog.pos.x, frog.pos.y);
		}
		
		private function jumpEase(value:Number):Number
		{
			return ((4 * value) * (1 - value) * JUMP_SCALE);
		}
		
		public function drown():void
		{
			var newGraphic : MovieClip = new GFX_FrogFlipped();
			newGraphic.rotation = graphic.rotation;
			
			newGraphic.blendMode = BlendMode.LAYER;
			
			var parent : DisplayObjectContainer = graphic.parent;
			graphic.parent.removeChild(graphic);
			parent.addChild(newGraphic);			
			
			graphic = newGraphic;
		}
	}
}